package com.guchenbo.example.dubbo;

import com.google.common.collect.Lists;
import com.guchenbo.example.dubbo.cluster.GcbCluster;
import com.guchenbo.example.dubbo.cluster.GcbDirectory;
import com.guchenbo.example.dubbo.rpc.protocol.GcbProtocol;
import com.guchenbo.example.dubbo.rpc.proxy.JdkProxyFactory;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Protocol;
import org.apache.dubbo.rpc.cluster.Cluster;
import org.apache.dubbo.rpc.cluster.Directory;
import org.junit.Test;

import java.util.List;

public class ClusterTest extends DubboBaseTest{

  private static Protocol protocol = new GcbProtocol();

  @Test
  public void testLocalCluster() {
    URL url = new URL("test", "", 1, type.getName());

    Invoker<EchoService> provider = JdkProxyFactory.getInvoker(type, url, proxy);
    protocol.export(provider);

    Invoker<EchoService> c1 = protocol.refer(type, url);
    Invoker<EchoService> c2 = protocol.refer(type, url);
    List<Invoker<EchoService>> invokers = Lists.newArrayList(c1, c2);

    Cluster cluster = new GcbCluster();
    Directory<EchoService> directory = new GcbDirectory<>(url, invokers);
    Invoker<EchoService> invoker = cluster.join(directory);

    EchoService service = JdkProxyFactory.getProxy(invoker);

    for (int i = 0; i < 10; i++) {
      service.echo("hello");
    }
  }
}
